Skip to main content

Env variable

1. Overview#

CellularJS provide a built-in package for managing env variables - @cellularjs/env . It make use of dotenv-flow to help you get configuration data from env file, let's learn how to use it.

"dotenv-flow extends dotenv adding the ability to have multiple .env files like .env.development, .env.test and .env.production, also allowing defined variables to be overwritten individually in the appropriate .env.local file.
Storing configuration in environment variables separate from code and grouping them by environments like development, test and production is based on The Twelve-Factor App methodology." - Note from "dotenv-flow".

2. Installation#

npm install @cellularjs/env

3. API#

tip

You can learn how to use this package by follow this link https://github.com/cellularjs/http-sample.

3.1. EnvModule.config#

Currently, EnvModule.config main feature is allow you to define specific token that will be used for resolve env variables.

Example:

import { Module, Container } from '@cellularjs/di';import { EnvModule } from '@cellularjs/env';
class YourEnv {}
interface YourEnv {  PORT: number;}
(async () => {  @Module({    exports: [EnvModule.config({ token: YourEnv })]  })  class CommonModule {}
  const container = new Container();  await container.addModule(CommonModule);
  const env = await container.resolve<YourEnv>(YourEnv);})();

3.2. env function#

This function allow you to get enviroment variables from env file only.

note

It is important to note that env function only work after EnvModule.config is invoked.

Example: after EnvModule.config is invoked, you can use env function like this:

import { env } from '@cellularjs/env';
// your setup ...
env<YourEnv>().PORT;